package com.dfire;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.Log4JLogger;
import org.apache.hadoop.fs.Path;
import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;


/**
 * 文件上传
 * @author <a href="mailto:huoguo@2dfire.com">火锅</a>
 * @time 18/7/2
 */
public class FileUpLoad {

    private static Log log =  LogFactory.getFactory().getInstance(FileUpLoad.class);

    // 扫描的文件目录
    private static String localDir = "/Users/qqr/Downloads/hive-conf";
    // 文件目录
    private static String upLoadDir = "hdfs://hadoop1006:8020/tmp";

    public static void main(String[] args) throws Exception {
        if(args.length != 0){
            localDir = args[0];
            upLoadDir = args[1];
        }
        System.out.println(localDir+":"+upLoadDir);
        Configuration conf = new Configuration();
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");

        FileSystem fs = FileSystem.get(URI.create(upLoadDir), conf);
        List<Path> pathList = getLocalFiles();
        upLoadFile(fs,pathList);
    }

    private static List<Path> getLocalFiles(){
        File localFile = new File(localDir);
        List<Path> list = new ArrayList<Path>();
        if(!localFile.exists()){
            throw new NullPointerException("Not found file : "+localDir);
        }

        if(localFile.isFile()){
            list.add(new Path(localFile.getPath()));
            return list;
        }else {
            File[] files = localFile.listFiles();
            for(File file : files){
                list.add(new Path(file.getPath()));
            }
        }

        return list;
    }


    public static void upLoadFile(FileSystem  fs,List<Path> list) throws IOException {
        for(Path srcPath : list){
            Path dstPath = new Path(upLoadDir);
            fs.copyFromLocalFile(false, srcPath, dstPath);
            System.out.println("上传成功："+srcPath);
        }
        fs.close();
    }

}
